package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.admin.annotation.ApplicationParam;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.admin.dao.BusinessMapper;
import cn.ibobo.lease.admin.service.BusinessService;
import cn.ibobo.lease.common.dto.BusinessDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


/**
 * 商户表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:38:03
 */
@Service
public class BusinessServiceImpl extends BaseServiceImpl<BusinessDTO, String> implements BusinessService {

    private static final Log LOGGER = LogFactory.getLog(BusinessServiceImpl.class);

    @Autowired
    private BusinessMapper mapper;
    @Autowired
    private ApplicationParam param;

    @Override
    protected BaseMapper<BusinessDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 商户表列表
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public List<BusinessDTO> list4Page(BusinessDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 商户表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public List<BusinessDTO> listByCondition(BusinessDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询商户表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public BusinessDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存商户表信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(BusinessDTO entity) {
        String bid = entity.getBid();
        if(bid == null || bid.length() == 0){
            String id = UuidUtil.getUuid();
            entity.setBid(id );
            entity.setCreatetm(DateUtil.getNow());
            //1. 生成二维码
            try {
                String dir = param.getUploadPath() + Constant.QR_PATH + "/temp.jpg";
                File file = new File(dir);
                QRCodeUtil.encode(id, new FileOutputStream(file));
                //2. 保存到映射位置
                String qrPath= UploadUtil.upLoadFile(FileUtil.fileToMultipartFile(file), param.getUploadPath(), Constant.QR_PATH);
                entity.setQrcode(qrPath);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultEnum.FAIL.getCode();
            }
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除商户表信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 生成/刷新商家二维码
     * @Author lililiang
     * @Date 2019/3/11 17:02
     * @Param [id]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer genQR(String id){

        String dir = param.getUploadPath() + Constant.QR_PATH + "/temp.jpg";
        String qrPath = "";
        File file = new File(dir);
        try {
            FileUtil.mkdirs(param.getUploadPath() + Constant.QR_PATH);
            if(!file.exists()){
                file.createNewFile();
            }
            //1. 生成二维码
            QRCodeUtil.encode(id, new FileOutputStream(file));
            //2. 保存到映射位置
            qrPath= UploadUtil.upLoadFile(FileUtil.fileToMultipartFile(file), param.getUploadPath(), Constant.QR_PATH);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEnum.FAIL.getCode();
        }
        //3. 保存到数据库
        BusinessDTO businessDTO = new BusinessDTO();
        businessDTO.setBid(id);
        businessDTO.setQrcode(qrPath);
        return mapper.edit(businessDTO);
    }
}
